package com.kgd.news.controller.admin;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.kgd.news.entity.Score;
import com.kgd.news.utils.BaseController;
import com.kgd.news.utils.StringUtils;

/**
 * @Email: huangsanm@foxmail.com
 * @Date: 2017/10/8
 * @Time: 下午5:30
 */
public class ScoreController extends BaseController {

    @Override
    public void list() {
        String score = getPara("score");
        String questionid = getPara("questionid");
        String querytype = "score";
        if (!StringUtils.isEmpty(questionid)) {
            setSessionAttr("questionid", questionid);
        } else {
            questionid = getSessionAttr("questionid");
        }
        String sql = "from score where 1 = 1";
        if (!StringUtils.isEmpty(questionid)) {
            sql += " and questionid = " + questionid;
            querytype += score;

        }
        if (!StringUtils.isEmpty(score)) {
            sql += " and s_score <= " + score + " and e_score >=" + score;
            querytype += score;

        }
        sql += " order by id desc";
        Page<Score> data = Score.dao.paginate(getPageIndex(querytype), PAGE_SIZE, "select *", sql);
        setAttr("data", data);
        render("score_list.html");
    }

    @Override
    public void edit() {
        Integer id = getParaToInt("id");
        Score score = Score.dao.findById(id);
        setAttr("score", score);
        render("score_edit.html");
    }

    @Override
    public void save() {
        Score sd = getModel(Score.class, "score");
        sd.setQuestionid(Long.valueOf(getSessionAttr("questionid")));
        if(sd != null && sd.getId() != null){
            sd.update();
        }else{
            sd.save();
        }

        list();
    }

    @Override
    public void del() {
        String ids = getPara("ids");
        Db.update("delete from score where id in (" + ids + ")");

        list();
    }
}
